package br.com.adimoveis.ui;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainView implements IMainView {

	private MainFrame frame;
	
	private AbstractCompositeView viewCorrente;
	
	private Map<String, AbstractCompositeView> views;
	
	public MainView(List<AbstractCompositeView> allViews) {
		views = new HashMap<String, AbstractCompositeView>();
		
		frame = new MainFrame();
		
		frame.clearBtn.setText("");
		frame.clearBtn.setIcon(new ImageIcon(ClassLoader.getSystemResource("edit-clear.png")));
		frame.clearBtn.setToolTipText("limpar busca");
		
		frame.salvarBtn.setText("");
		frame.salvarBtn.setIcon(new ImageIcon(ClassLoader.getSystemResource("media-floppy.png")));
		frame.salvarBtn.setToolTipText("salvar");
		
		frame.novoBtn.setText("");
		frame.novoBtn.setIcon(new ImageIcon(ClassLoader.getSystemResource("document-new.png")));
		frame.novoBtn.setToolTipText("novo");
		
		frame.excluirBtn.setText("");
		frame.excluirBtn.setIcon(new ImageIcon(ClassLoader.getSystemResource("mail-mark-not-junk.png")));
		frame.excluirBtn.setToolTipText("remover");
		
		frame.editarBtn.setText("");
		frame.editarBtn.setIcon(new ImageIcon(ClassLoader.getSystemResource("accessories-text-editor.png")));
		frame.editarBtn.setToolTipText("ediatar");
		
		addViews(allViews);
		
		frame.addSelectionListener(new ChangeListener(){

			public void stateChanged(ChangeEvent e) {
				JTabbedPane tabbed = (JTabbedPane) e.getSource();
				int selectedIndex = tabbed.getSelectedIndex();
				String title = tabbed.getTitleAt(selectedIndex);
				
				AbstractCompositeView selectedView = views.get(title);
				viewCorrente.turnListenersOff();
				viewCorrente = selectedView;
				viewCorrente.turnListenersOn();
			}
			
		});
	}



	private void addViews(List<AbstractCompositeView> allViews) {
		
		if(views.isEmpty() && allViews.size() > 0){
			this.viewCorrente = allViews.get(0);
		}
		
		for(AbstractCompositeView compositeView : allViews){
			
			if(views.containsKey(compositeView.getTitle())){
				throw new IllegalArgumentException("Abas com nomes duplicados");
			}
			
			frame.addTabbedComponent(compositeView.getIcon(), compositeView.getTitle(), compositeView.getComponent());
			for(IView view : compositeView.getSubViews()){
				view.addMainframe(frame);
			}
			views.put(compositeView.getTitle(), compositeView);
		}
		
		viewSetUp();
	}



	private void viewSetUp() {
		if(this.viewCorrente != null){
			this.viewCorrente.turnListenersOn();
		}
	}



	public void show() {
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				
				frame.setMinimumSize(new Dimension(800,600));
				frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				SwingUtilities.updateComponentTreeUI(frame);
				SwingUtilities.updateComponentTreeUI(frame.jMenuBar1);
				
			}
		});
	}
}
